/*
 *  Copyright 2006 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.persistence.hibernate.util;

import com.blandware.atleap.model.core.ContentResource;
import com.blandware.atleap.persistence.core.ContentResourceDAO;
import org.hibernate.event.DeleteEvent;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;

/**
 * <p>Processor that processes entity deletion.
 * </p>
 * <p><a href="CoreDeleteProcessor.java.html"><i>View source</i></a></p>
 *
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 *         &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.2 $ $Date: 2006/10/03 13:47:42 $
 */
public class CoreDeleteProcessor
        implements DeleteProcessor, ApplicationContextAware {

    /**
     * Logging instance
     */
    protected transient final Log log = LogFactory.getLog(CoreDeleteProcessor.class);

    /**
     * Application context
     */
    protected ApplicationContext applicationContext;
    /**
     * DAO to work with content resources
     */
    protected ContentResourceDAO contentResourceDAO = null;
    /**
     * Name of bean that represents content resource DAO
     */
    protected String contentResourceDAOBeanName;

    /**
     * Sets application context.
     *
     * @param applicationContext context to set
     * @throws BeansException
     */
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
     * Sets name of bean that represents content resource DAO.
     *
     * @param contentResourceDAOBeanName bean name
     */
    public void setContentResourceDAOBeanName(
            String contentResourceDAOBeanName) {
        this.contentResourceDAOBeanName = contentResourceDAOBeanName;
    }

    /**
     * Processes entity deletion.
     *
     * @param event event occured
     */
    public void processDelete(DeleteEvent event) {
        if (contentResourceDAO == null) {
            initialize();
        }

        Object entity = event.getObject();
        if (entity instanceof ContentResource) {
            ContentResource contentResource = (ContentResource) entity;
            contentResourceDAO.removeAssociations(contentResource);
        }
    }

    /**
     * Initializes object.
     */
    protected void initialize() {
        contentResourceDAO = (ContentResourceDAO) applicationContext.getBean(contentResourceDAOBeanName);
    }
}
